package com.mes.eventhandler;

import com.mes.data.EmesPBomItemDiy;
import org.openbravo.base.model.Entity;
import org.openbravo.base.model.ModelProvider;
import org.openbravo.base.model.Property;
import org.openbravo.client.kernel.event.EntityNewEvent;
import org.openbravo.client.kernel.event.EntityPersistenceEventObserver;
import org.openbravo.client.kernel.event.EntityUpdateEvent;

import javax.enterprise.event.Observes;

public class EmesPBomDiyEventHandler extends EntityPersistenceEventObserver {
	private static Entity[] entities = { ModelProvider.getInstance().getEntity(EmesPBomItemDiy.ENTITY_NAME) };
	
	@Override
	protected Entity[] getObservedEntities() {
		return entities;
	}

	public void onUpdate(@Observes EntityUpdateEvent event) throws Throwable {
		if (!isValidEvent(event)) {
			return;
		}

		try {
			//获取计划
			EmesPBomItemDiy Diy = (EmesPBomItemDiy) event.getTargetInstance();

			// 将要更改的实体类
			final Entity detailsEntity = ModelProvider.getInstance().getEntity(EmesPBomItemDiy.ENTITY_NAME);
			//本次图案存储路径
			final Property filePath = detailsEntity.getProperty(EmesPBomItemDiy.PROPERTY_FILEPATH);
			//本次图案存储名字
			//final Property fileName = detailsEntity.getProperty(EmesPBomItemDiy.PROPERTY_FILENAME);

			if(Diy.getEmesShape() != null && !Diy.getEmesShape().equals("")){
				//获取图案名字
				String FileNames = Diy.getEmesShape().getFileName();
				//获取图案路径
				String FilePaths = Diy.getEmesShape().getFilePath();

				event.setCurrentState(filePath, FilePaths);
				//event.setCurrentState(fileName, FileNames);
			}


		} catch (Exception e) {
			e.printStackTrace();
		}

	}


	public void onSave(@Observes EntityNewEvent event) {
		if (!isValidEvent(event)) {
			return;
		}


		try {
			//获取计划
			EmesPBomItemDiy Diy = (EmesPBomItemDiy) event.getTargetInstance();

			// 将要更改的实体类
			final Entity detailsEntity = ModelProvider.getInstance().getEntity(EmesPBomItemDiy.ENTITY_NAME);
			//本次图案存储路径
			final Property filePath = detailsEntity.getProperty(EmesPBomItemDiy.PROPERTY_FILEPATH);
			//本次图案存储名字
			//final Property fileName = detailsEntity.getProperty(EmesPBomItemDiy.PROPERTY_FILENAME);

			if(Diy.getEmesShape() != null && !Diy.getEmesShape().equals("")){
				//获取图案名字
				String FileNames = Diy.getEmesShape().getFileName();
				//获取图案路径
				String FilePaths = Diy.getEmesShape().getFilePath();

				event.setCurrentState(filePath, FilePaths);
				//event.setCurrentState(fileName, FileNames);
			}


		} catch (Exception e) {
			e.printStackTrace();
		}


	}

}
